import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {Router} from '@angular/router';
import {HttpClient} from '@angular/common/http';
import {CudTableService} from '../../../public/serve/cud-table/cud-table.service';
import {Service} from '../../../server/server';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import {HintsService} from '../../../public/serve/hints/hints.service';
import { pushedAddType } from '../pushed.data';

declare var $:any;

@Component({
  selector: 'app-pushed-table',
  templateUrl: './pushed-table.component.html'
})

export class PushedTableComponent implements OnInit {

    total      : number = 0;
    naturalNum : number = 0;
    startTime : String = '';
    endTime : String = '';
    details    : object;
    isSelectAll : boolean = false;
    isChose     : boolean = false;
    public isSelect: boolean = true;
    public isDisable: any;              // 按钮（启用/禁用）

    public totalItems: number;

    public tabVal: any;
    public rolesData: any[];
    right: any = {
        "systemPushed": {},
    };

    constructor( public removeTableService: RemoveTableService,private http: HttpClient, private hintsService: HintsService, private server: Service, private router: Router, private tableListService: CudTableService) {
        this.right = $.extend(this.right, server.getRight());
    }


    @Input() searchData: any;
    @Input() tableNumber: number;

    ngOnInit() {
        this.getTabList(1, 20, '0', this.searchData);
    }

    @Output()
    tabeListOutput: EventEmitter<string> = new EventEmitter();

    @Output()
    detailsOutput: EventEmitter<pushedAddType> = new EventEmitter();

    // 表格
    public getTabList(pageNumber, pageSize, id, queryCriteria) {
        let url = this.server.baseUrl + 'Service/ThemeWords/getThemeTablesList';
        let data = { pageNo: pageNumber, pageSize: pageSize, id: id, queryCriteria: queryCriteria }
        this.tableListService.postRequest(url, data).then((data: any) => {
            if(data.result_code == '0000') {
                this.tabVal = data.result;
                this.tabeListOutput.emit(data.length);
                this.totalItems = data.length;
            }else {
                this.hintsService._operationHints(data.message, data.result_code);
            }

        }).catch();
    }

    @Output() isSelectFromTable = new EventEmitter<boolean>();

    // 每一行的表格信息
    outputTabVal(tabval) {
        let tabVal: pushedAddType = {
            parentId: tabval.parentId,
            child:  tabval.child,
            name:  tabval.name,
            status:  tabval.status,
            create_by : tabval.create_by,
            createTime: tabval.createTime,
            description: tabval.description,
            themeGroupId: tabval.themeGroupId,
            themeTableId: tabval.themeTableId
        };
        this.detailsOutput.emit(tabVal);
    }

    /**
     * 全选
     * @param bool
     * @param tabVal
     */
    _selectAll(bool, tabVal) {
        this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'themeTableId');
        this.isSelect = !this.isSelectAll;
        this.isSelectFromTable.emit(this.isSelect);
    }

    /**
     * 单选（多选）
     * @param bool
     * @param scopesId
     * @param length
     */
    _chose(bool, scopesId, length) {
        this.isSelectAll = this.removeTableService.removeChose(bool, scopesId, length)['bool'];
    }

    /**
     * 重置表格复选框、选中的ID
     */
    private _cancelChecked() {
        this.isChose = this.isSelectAll = false;
        this.removeTableService._clearId('scopesId');
    }



    /**
     * 详情（弹窗）
     * @param info
     */
    _details(info: object) {
        this.outputTabVal(info);
        $("#details").modal("show");
    }

    /**
     * 修改（弹窗）
     * @param info
     */
    _edit(info: object) {
        this.outputTabVal(info);
        $("#edit").modal("show");
    }


}
